package com.siburian.MyStuff;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import android.util.Log;


public class Bab_Pelajaran {
	private static final String ROW_ID = "ID_BAB";
	private static final String ID_PELAJARAN = "ID_Pelajaran";
	private static final String ID_KELAS = "ID_Kelas";
	private static final String URUTAN = "Urutan";
	private static final String TEKS = "Teks";
	private static final String GAMBAR = "Gambar";
	private static final String SUARA = "Suara";
	 
	private static final String NAMA_DB = "BabPelajaranDB";// nama database
	private static final String NAMA_TABEL = "Bab";// nama tabel
	private static final int DB_VERSION = 1;
 
	private static final String CREATE_TABLE_PROFILE = "create table "+NAMA_TABEL+" ("+ROW_ID+" integer,"+ID_PELAJARAN+" integer, "+ID_KELAS+" integer, "+URUTAN+" integer PRIMARY KEY autoincrement, "+TEKS+" text,"+GAMBAR+" BLOB, "+SUARA+" text)";
	private final Context context;
	private DatabaseOpenHelper dbHelper;
	private SQLiteDatabase db;
 
	public Bab_Pelajaran(Context ctx) {
		this.context = ctx;
		dbHelper = new DatabaseOpenHelper(context);
		db = dbHelper.getWritableDatabase();
	}
 
	private static class DatabaseOpenHelper extends SQLiteOpenHelper {
		 
		public DatabaseOpenHelper(Context context) {
			super(context, NAMA_DB, null, DB_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(CREATE_TABLE_PROFILE);
		}
 
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS "+NAMA_TABEL);
			onCreate(db);
 
		}
	}
 
	public void close() {
		dbHelper.close();
	}
 
	public void addContentPelajaran(int idPelajaran,int idKelas,String teks,byte[] image, String suara) {
 
		ContentValues values = new ContentValues();
		values.put(ID_PELAJARAN, idPelajaran);
		values.put(ID_KELAS, idKelas);
		values.put(TEKS, teks); 
		values.put(GAMBAR, image);
		values.put(SUARA, suara);
		try {
 
		//	db.delete(NAMA_TABEL, null, null);
			db.insert(NAMA_TABEL, null, values);
		} catch (Exception e) {
			Log.e("DB ERROR", e.toString());
			e.printStackTrace();
		}
	}
	
	
	public ArrayList<ArrayList<Object>> getDataContent(){
		ArrayList<ArrayList<Object>> dataArray = new ArrayList<ArrayList<Object>>();
		Cursor cur;
		try {
			cur = db.query(NAMA_TABEL,
					new String[] { ROW_ID,ID_PELAJARAN,ID_KELAS, TEKS,GAMBAR,SUARA }, null, null,
					null, null, null);
			cur.moveToFirst();
			if (!cur.isAfterLast()) {
				do {
					ArrayList<Object> dataList = new ArrayList<Object>();
					dataList.add(cur.getLong(0));
					dataList.add(cur.getString(1));
					dataList.add(cur.getString(2));
					dataList.add(cur.getString(3));
					dataList.add(cur.getString(4));
 
					dataArray.add(dataList);
 
				} while (cur.moveToNext());
 
			}
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("DEBE ERROR", e.toString());
		}
		return dataArray;
	}

	public ArrayList<ArrayList<Object>> getBacaan(int idBab,int idPelajaran,int idKelas){
		ArrayList<ArrayList<Object>> dataArray = new ArrayList<ArrayList<Object>>();
		Cursor cur;
		try {
			cur = db.query(NAMA_TABEL,
					new String[] { URUTAN, TEKS,GAMBAR,SUARA }, ROW_ID + "=" + '"' + idBab + '"'+ " and " + ID_PELAJARAN+ "=" + '"' + idPelajaran + '"'+ " and " + ID_KELAS+ "=" + '"' + idKelas + '"', null,
					null, null, null);
			cur.moveToFirst();
			if (!cur.isAfterLast()) {
				do {
					ArrayList<Object> dataList = new ArrayList<Object>();
					dataList.add(cur.getLong(0));
					dataList.add(cur.getString(1));
					dataList.add(cur.getBlob(2));
					dataList.add(cur.getString(3));
 
					dataArray.add(dataList);
 
				} while (cur.moveToNext());
 
			}
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("DEBE ERROR", e.toString());
		}
		return dataArray;
	}
	
	public byte[] getImageFromDatabase(int idUrutan){
		byte[] image = null;
		Cursor cur;
		try {
			cur = db.query(NAMA_TABEL,
					new String[] {URUTAN, TEKS,GAMBAR,SUARA },  URUTAN + "=" + '"' + idUrutan + '"', null,
					null, null, null);
			cur.moveToFirst();
//			if (!cur.isAfterLast()) {
//				do {
//					ArrayList<Object> dataList = new ArrayList<Object>();
//					image = cur.getBlob(2);
//				} while (cur.moveToNext());
//			}
            if(cur.moveToFirst()) {
                image = cur.getBlob(cur.getColumnIndexOrThrow(GAMBAR));
        }
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("DEBE ERROR", e.toString());
		}
		return image;
	}
	
	
	public void generateBab(byte[] image1,byte[] image2,byte[] image3,byte[] image4,byte[] image5,byte[] image6,byte[] image7,byte[] image8,byte[] image9,byte[] image10,byte[] image11){
		ContentValues values = new ContentValues();
		values.put(ROW_ID, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(TEKS, "Kuku"); 
		values.put(GAMBAR, image1);
		values.put(SUARA, "FingerNail");
		db.insert(NAMA_TABEL, null, values);
		
		values.put(ROW_ID, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(TEKS, "Tangan"); 
		values.put(GAMBAR, image2);
		values.put(SUARA, "Hand");
		db.insert(NAMA_TABEL, null, values);
		
		values.put(ROW_ID, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(TEKS, "Mulut"); 
		values.put(GAMBAR, image3);
		values.put(SUARA, "Mouth");
		db.insert(NAMA_TABEL, null, values);
				
		values.put(ROW_ID, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(TEKS, "Burung"); 
		values.put(GAMBAR, image4);
		values.put(SUARA, "Bird");
		db.insert(NAMA_TABEL, null, values);
		
		values.put(ROW_ID, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(TEKS, "Telinga"); 
		values.put(GAMBAR, image5);
		values.put(SUARA, "Ear");
		db.insert(NAMA_TABEL, null, values);
		
		
		values.put(ROW_ID, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(TEKS, "Hidung"); 
		values.put(GAMBAR, image4);
		values.put(SUARA, "Nose");
		db.insert(NAMA_TABEL, null, values);
		
		values.put(ROW_ID, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(TEKS, "Minuman"); 
		values.put(GAMBAR, image4);
		values.put(SUARA, "Drink");
		db.insert(NAMA_TABEL, null, values);
		
		values.put(ROW_ID, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(TEKS, "Jari"); 
		values.put(GAMBAR, image4);
		values.put(SUARA, "Finger");
		db.insert(NAMA_TABEL, null, values);
		
		values.put(ROW_ID, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(TEKS, "Mata"); 
		values.put(GAMBAR, image4);
		values.put(SUARA, "Eyes");
		db.insert(NAMA_TABEL, null, values);
		
		values.put(ROW_ID, 1);
		values.put(ID_PELAJARAN, 1);
		values.put(ID_KELAS, 1);
		values.put(TEKS, "Gigi"); 
		values.put(GAMBAR, image4);
		values.put(SUARA, "Tooth");
		db.insert(NAMA_TABEL, null, values);
	}
}
